package com.stack.auto_parts.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stack.auto_parts.common.ApiResult;
import com.stack.auto_parts.common.exception.BusinessException;
import com.stack.auto_parts.entity.User;
import com.stack.auto_parts.mapper.UserMapper;
import com.stack.auto_parts.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Title: UserServiceImpl
 * @Author WUYU_MIN
 * @Package com.stack.auto_parts.service.impl
 * @Date 2024/7/8 18:43
 * @description:
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public String register(User user) {

        // QueryWrapper 条件构造器
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", user.getPhone());
        if(userMapper.selectOne(queryWrapper) != null){
            return "该用户已经存在了";
        }else {
            user.setUuid(UUID.randomUUID().toString());
            Integer integer = userMapper.insert(user);
            if (integer != 1) {
                return "用户注册失败";
            }
        }
        return null;
    }


    /**
     * 删除指定User
     * @param
     * @return
     */
    @Override
    public String deleteUser(String uuid) {
        // QueryWrapper 条件构造器
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid", uuid);
        if(userMapper.selectOne(queryWrapper) == null){
            return "该用户不存在";
        }
        else {
            Integer integer = userMapper.deleteById(userMapper.selectOne(queryWrapper));
            if(integer != 1)
                return "用户删除失败";
        }
        return null;
    }

    @Override
    public List<User> selectAll() {
        List<User> userList =  new ArrayList<>();
        userList = userMapper.selectList(null);
        return userList;
    }

    @Override
    public List<User> selectBycondition(String user_name, String phone, String role) {
        System.out.println(user_name +"  "+ phone+ "   " + role);
        List<User> userList = userMapper.listBycondition(user_name,phone,role);

        System.out.println(userList);
        return userList;
    }

    @Override
    public String updateUser(User user) {
        // QueryWrapper 条件构造器
        // 查询当前待更新用户是否存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid", user.getUuid());
        User currentUser = userMapper.selectOne(queryWrapper);

        if (currentUser == null) {
            return "该用户不存在";
        }
        user.setId(currentUser.getId());
        // 查询当前用户表中是否存在相同姓名的记录
        QueryWrapper<User> queryWrapperExisting = new QueryWrapper<>();
        queryWrapperExisting.ne("uuid", user.getUuid()); // 排除当前用户自身
        queryWrapperExisting.eq("user_name", user.getUserName()); // 根据用户名查询
        queryWrapperExisting.eq("phone", user.getPhone()); // 当前用户电话
        User existingProduct = userMapper.selectOne(queryWrapperExisting);

        if (existingProduct != null) {
            return "修改后的姓名已存在，修改失败";
        }
        // 更新零件信息
        Integer rowsAffected = userMapper.updateById(user);
        if (rowsAffected != 1) {
            return "用户信息修改失败";
        }
        return null;
    }

    @Override
    public User login(String phone, String password) {

        return userMapper.getByUserNameAndPassword(phone,password);
    }
}
